import {
  createRouter,
  createWebHashHistory
} from "vue-router";
import {
  ElMessage
} from "element-plus";

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: "/login",
    name: "login",
    component: () =>
      import( /* webpackChunkName: "about" */ "../views/Login.vue"),
  },
  {
    path: "/index",
    name: "index",
    component: () =>
      import( /* webpackChunkName: "about" */ "../views/Index.vue"),
    // children: [{
    //     path: "privacy",
    //     name: "privacy",

    //     component: () =>
    //       import( /* webpackChunkName: "about" */ "../views/Privacy.vue"),
    //   },
    //   {
    //     path: "tc",
    //     name: "tc",

    //     component: () =>
    //       import( /* webpackChunkName: "about" */ "../views/TandC.vue"),
    //   }, {
    //     path: "about",
    //     name: "about",

    //     component: () =>
    //       import( /* webpackChunkName: "about" */ "../views/About.vue"),
    //   }
    // ]
  },
];


function getNav() {

  return [{
      path: "privacy",
      name: "privacy",
      component: "Privacy"
    },
    {
      path: "about",
      name: "about",
      component: "About"
    },
    {
      path: "tc",
      name: "tc",
      component: "TandC"
    },
    {
      path: "admin",
      name: "admin",
      component: "Admin"
    },
    {
      path: "stationAssetLink",
      name: "stationAssetLink",
      component: "StationAssetLink"
    },
    {
      path: "dashboard",
      name: "dashboard",
      component: "Dashboard"
    },
    {
      path: "home",
      name: "home",
      component: "Home"
    },
    {
      path: "access",
      name: "access",
      component: "Access"
    },
    {
      path: "audit",
      name: "audit",
      component: "Audit"
    },
    {
      path: "profile",
      name: "profile",
      component: "Profile"
    }
  ]

}

// 路由数据存起来
localStorage.setItem("routers", JSON.stringify(getNav()))


const router = createRouter({
  history: createWebHashHistory(),
  routes,
});




// router.beforeEach(async (to, from, next) => {
//   // 获取动态路由
//   // 解决强制刷新页面，消失的问题
//   if (to.matched.length == 0) {
//     router.push(to.path);
//   }
//   next()
//   if (localStorage.getItem("token")) {
//     // 添加动态路由
//     let res = await getNav();
//     addDynamicRoutes(res);

//     if (to.path === '/login') {
//       next('/')
//     } else {
//       next()
//     }
//   } else {
//     console.log("进来这一步");
//     next({
//       path: '/login',
//     });
//   }
// })

router.beforeEach(async (to, from, next) => {

  // 解决强制刷新页面，消失的问题
  if (to.matched.length == 0) {
    router.push(to.path);
  }

  // 获取token
  const token = localStorage.getItem("token");
  // 有token
  if (token) {
    // 添加动态路由
    let res = await getNav();
    addDynamicRoutes(res);
    next();
  } else {
    // 否则是没有
    // 如果去的是登录页
    if (to.path === "/login") {
      // 直接放行
      next();
    } else if (to.path === "/register") {
      // 直接放行
      next();
    } else {
      // 如果去的是其他页,跳转到登录页
      ElMessage({
        showClose: true,
        message: "please login",
        center: true,
      });
      // 跳转到登录页
      return next({
        path: "/login"
      });
    }
  }
});









export function addDynamicRoutes(result) {

  result.forEach(v => {
    router.addRoute("index", {
      path: v.path,
      name: v.name,
      meta: {
        titie: v.title
      },
      component: () => import( /* webpackChunkName: "about" */ `@/views/${v.component}`),
    })
  })

}



export default router;